package com.siblings.loan.ui.loan.impl

import android.content.Context
import com.siblings.loan.http.IHttpCallback
import com.siblings.loan.ui.loan.view.ISibLoanOrderView
import com.siblings.mvp.presenter.MvpPresenter
import org.json.JSONArray
import org.json.JSONObject

/**
 * Siblings
 * @Author wwdeng
 * @CreateTime: 2024/7/4 15:46
 * @Annotation：贷款订单 Presenter 实现
 */
class SibLoanOrderPresenterImpl(context: Context?) : MvpPresenter<ISibLoanOrderView>(context) {
    private var model: SibLoanOrderModelImpl? = null

    //所有权限
    private var allPermissions: ArrayList<String>? = null

    init {
        model = SibLoanOrderModelImpl()
    }

    /**
     * 获取用户贷款额度信息
     */
    fun loadSibLoanOrderListAction(){
        model!!.loadSibLoanOrderListAction(object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        if (obj != null) {
                            val loanList = obj.optJSONArray("loanList")
                            val orderList = ArrayList<JSONObject>()
                            if (loanList != null && loanList.length() > 0) {
                                for (i in 0..<loanList.length()){
                                    val item = loanList.optJSONObject(i)
                                    //非待还款不显示
                                    if ("WAIT_REPAY" != item.optString("orderStatus")){
                                        continue
                                    }
                                    val titleList = JSONArray()
                                    val contentList = JSONArray()
                                    val array = item.optJSONArray("list")
                                    if (array != null) {
                                        for (index in 0..<array.length()){
                                            val model = array.optJSONObject(index)
                                            if (model.has("title") && "YES" == model.optString("title")){
                                                titleList.put(model)
                                            } else {
                                                contentList.put(model)
                                            }
                                        }
                                    }
                                    item.put("titleList",titleList)
                                    item.put("contentList",contentList)
                                    orderList.add(item)
                                }
                                mvpView.showSibOrderList(orderList)
                            }
                            if (orderList.size == 0){
                                mvpView.showSibEmptyView()
                            }
                        }
                    }
                    else -> {
                        mvpView.showSibEmptyView()
                        mvpView.showSibToast(data.optString("msg"))
                    }
                }
            }
            override fun onRequestError(error: JSONObject) {
                mvpView.showSibToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView.hideSibLoading()
            }
        })
    }

    /**
     * 还款
     */
    fun onSibOrderPayLinkAction(orderId: String){
        mvpView.showSibLoading()
        model!!.onSibOrderPayLinkAction(orderId,object : IHttpCallback{
            override fun onRequestSuccess(data: JSONObject) {
                when(data.optInt("code")) {
                    0 -> {//成功
                        val obj = data.optJSONObject("obj")
                        if (obj != null){
                            val repayUrl = obj.optString("repayUrl")
                            val url = obj.optString("url")
                            mvpView.showSibOrderPayLink(repayUrl)
                        }
                    }
                    else -> {
                        mvpView.showSibToast(data.optString("msg"))
                    }
                }
            }

            override fun onRequestError(error: JSONObject) {
                mvpView.showSibToast(error.optString("errorMsg"))
            }

            override fun onRequestFinish() {
                mvpView.hideSibLoading()
            }
        })
    }
}